﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace rk4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Licz2();
        }

        private double k = 0.1;
        private double r = 3.72007597602084E-44;
        private double dt = 1.0;
        private double t = 1000.0;

        private void Licz2()
        {
            double k1, k2, k3, k4;

            for (t = 1000; t < 1500; t += dt)
            {
                textBox1.Text += t + "\t" + r + "\t" + Math.Exp(-k*t) + "\r\n";

                k1 = f(r) * dt;
                k2 = f(r + 0.5 * k1) * dt;
                k3 = f(r + 0.5 * k2) * dt;
                k4 = f(r+k3) * dt;

                r = r + (1 / (float)6) * (k1 + 2 * k2 + 2 * k3 + k4);
            }
        }

        private double f(double r)
        {
            return (-(k*r));
        }

        private void Licz()
        {
            int m = 10000;

            double t, dt, k;
            double rm1, r, rp1, rp2;
            int i;

            dt = 1.0;
            k = 0.1;

            rm1 = 1;

            r = rm1;

            if (m != 0)
            {
                for (i = 0; i < m; i++)
                {
                    r = r - k * r * dt / (double)m;
                }
            }
            else
            {
                r = r - k * r * dt;
            }

            for (t = 0; t < 900; t += dt)
            {
                rp1 = rm1 - (k * r) * 2 * dt;
                rp2 = r - (k * rp1) * 2 * dt;

                rm1 = r;
                r = rp1;
                rp1 = rp2;

                textBox1.Text += t + "\t" + rp2 + "\r\n";
            }
        }
    }
}
